package com.lib.torment.androidlib.widget;

import android.content.Context;
import android.support.annotation.ColorRes;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.lib.torment.androidlib.ui.AppActivity;

/**
 * Created by Torment on 2016/12/30.
 */

public class MaterialProgressView extends ImageView {
    private MaterialProgressDrawable mMaterialProgressDrawable;

    public MaterialProgressView(Context context) {
        super(context);
    }

    public MaterialProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MaterialProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mMaterialProgressDrawable = new MaterialProgressDrawable(context, this);
        mMaterialProgressDrawable.setBackgroundColor(0xFFFFFFFF);
        mMaterialProgressDrawable.updateSizes(MaterialProgressDrawable.LARGE);
        mMaterialProgressDrawable.setAlpha(255);
        setColorSchemeResources(AppActivity.SWIPE_REFRESH_COLOR);

        setImageDrawable(mMaterialProgressDrawable);
        mMaterialProgressDrawable.start();
    }

    public void setColorSchemeResources(@ColorRes int... colorResIds) {
        final Context context = getContext();
        int[] colorRes = new int[colorResIds.length];
        for (int i = 0; i < colorResIds.length; i++) {
            colorRes[i] = ContextCompat.getColor(context, colorResIds[i]);
        }
        mMaterialProgressDrawable.setColorSchemeColors(colorRes);
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == VISIBLE) {
            mMaterialProgressDrawable.start();
        } else {
            mMaterialProgressDrawable.stop();
        }
    }
}
